#!/usr/bin/Rscript
options(stringsAsFactors = F);
if(interactive()) {
  rm(list=ls());
  graphics.off();
  clr <- function () {
    source("/home/sco/mnt/smoke/clip", print.eval = T);
  }
  sts <- function () {
    source("code/peak_width_cleaned.r", print.eval = T);
  }
}

# {{{ library(tidyverse);
if(exists("read_csv")) {
  ltv <- 1;
} else {
  library("tidyverse");
}
# }}}

# Below, edit to something which exists!
outdir <- c("rout"); 

# Empty data frame outdf.
outdf <- data.frame(datfile = character(),
peak.micron = numeric(),
peak.intensity = numeric(),
half.intensity = numeric(),
left.micron = numeric(),
left.intensity = numeric(),
right.micron = numeric(),
right.intensity = numeric(),
peak.width = numeric()
);

all.df <- data.frame(micron = numeric(), intensity = numeric(),
strain = factor());

datfiles <- list.files(
  path = c("data/WT","data/SepH_mutant"),
  full.names = TRUE);
int.thresh <- 100;
incount <- 0L;
threshcount <- 0L;
for(txtfn in datfiles) { 
  cat(txtfn, "\n");
  bn <- basename(txtfn);
  bn <- sub("\\.txt$", "", bn);
  rdf <- read.table(txtfn, header = T, sep = "\t");
  incount <- incount + nrow(rdf);
  temp <- rdf[rdf$Y >= int.thresh,];
  threshcount <- threshcount + nrow(temp);
  if(grepl("^WT", bn)) { bnstr <- "WT"; } else {
    bnstr <- "SepH"; }
  all.df <- rbind(all.df, data.frame(datfile = bn, micron = rdf$X,
  intensity = rdf$Y, strain = rep(bnstr, nrow(rdf))));
  rdf <- data.frame(X = rdf$X, Y = rdf$Y);
  rdf$pk <- rep(FALSE, nrow(rdf));
  tipx <- numeric();
  tipy <- numeric();
  for(mid in seq(3, length(rdf$Y) - 2)) {
    ldx <- mid - 2; rdx <- mid + 2;
    y5 <- rdf$Y[ldx:rdx];
    if(any(y5 < 1)) { next; }
    oy5 <- order(y5, decreasing = T);
    if(oy5[1] == 3 & rdf[mid, "Y"] >= int.thresh) {
      rdf[mid, "pk"] <- T;
      tipx <- c(tipx, rdf$X[mid]);
      tipy <- c(tipy, rdf$Y[mid]);
    }
  }
  tip <- data.frame(tipx = tipx, tipy = tipy);
  
  pkdf <- rdf[rdf$pk == T,];

  for (pdx in as.integer(row.names(rdf[rdf$pk == T,]))) {
    peak.micron <- rdf[pdx, 1];
    peak.intensity <- rdf[pdx, 2];
    half.intensity <- peak.intensity / 2;

# move left
    intensity <- peak.intensity;
    leftdx <- pdx;
    while(intensity > half.intensity) {
      leftdx <- leftdx - 1;
      if(leftdx <= 1) { break; }
      intensity <- rdf[leftdx, 2];
    }

# move right
    intensity <- peak.intensity;
    rightdx <- pdx;
    while(intensity > half.intensity) {
      rightdx <- rightdx + 1;
      if(rightdx >= nrow(rdf)) { break; }
      intensity <- rdf[rightdx, 2];
    }

    left.micron <- rdf[leftdx, 1];
    left.intensity <- rdf[leftdx, 2];
    right.micron <- rdf[rightdx, 1];
    right.intensity <- rdf[rightdx, 2];

    peak.width <- right.micron - left.micron;

    outdf <- rbind(outdf, list(datfile = bn, peak.micron = peak.micron,
          peak.intensity = peak.intensity, half.intensity = half.intensity,
          left.micron = left.micron, left.intensity = left.intensity,
          right.micron = right.micron, right.intensity = right.intensity,
          peak.width = peak.width));
  }
}

outdf$strain <- rep("WT", nrow(outdf));
templog <- grepl("^SepH", outdf$datfile)
outdf[templog, "strain"] <- "SepH";
outdf %>% write_tsv(file.path(outdir, "peak_widths.txt"));

